package com.aigame.platform;

import com.aigame.activity.BaseActivity;
import com.aigame.activity.OrderListActivity;
import com.aigame.activity.PlatformActivity;
import com.aigame.activity.RetrievePasswordActivity;
import com.aigame.dao.OrderDao;
import com.aigame.dao.UserDao;
import com.aigame.entity.Order;
import com.aigame.listener.MiscCallbackListener.OnLoginProcessListener;
import com.aigame.listener.MiscCallbackListener.OnLogoutProcessListener;
import com.aigame.listener.MiscCallbackListener.OnPayProcessListener;
import com.aigame.socket.ConsumeSocket;
import com.aigame365.zhizunbao.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class GeneralPlatform extends BasePlatform {
	private static GeneralPlatform generalPlatform = null;
	public OnLoginProcessListener olc;
	public OnPayProcessListener opc;
	public OnLogoutProcessListener oloc;
	public GeneralPlatform() {
	}
	public static GeneralPlatform getInstance() {
		if (generalPlatform == null) {
			synchronized (GeneralPlatform.class) {
				generalPlatform = new GeneralPlatform();
			}
		}
		return generalPlatform;
	}
	public void logout(Context ctx, OnLogoutProcessListener oloc) {
		UserDao userDao = new UserDao(ctx);
		userDao.cleanLoginHistory();
		this.oloc = oloc;
		this.oloc.finishLogoutProcess(-1);
	}

	public void login(Context ctx, String appid, String partner,Boolean autoLogin, OnLoginProcessListener olc) {
		Intent in = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("appid", appid);
		bundle.putString("partner", partner);
		bundle.putBoolean("autoLogin", autoLogin);
		bundle.putInt("action_code", BaseActivity.LOGIN_CODE);
		in.putExtras(bundle);
		in.setClass(ctx, PlatformActivity.class);
		this.olc = olc;
		ctx.startActivity(in);
	}
//	public void register(Activity activity, String appid, String partner) {
//		Intent in = new Intent();
//		Bundle bundle = new Bundle();
//		bundle.putString("appid", appid);
//		bundle.putString("partner", partner);
//		in.putExtras(bundle);
//		in.setClass(activity, RegisterActivity.class);
//		activity.startActivityForResult(in, BaseActivity.REGISTER_CODE);
//	}
	public Boolean pay(Context ctx,Order order, OnPayProcessListener opc) {
//		ConsumeSocket cSocket = new ConsumeSocket();
//		OrderDao orderDao = new OrderDao(ctx);
//		orderDao.recordOrder(order);
//		String money = cSocket.consume(order);
//		if( money !=null) {
//			if(cSocket.checkOrder(order)) {
//				orderDao.finishOrder(order);
//				return true;
//			}
//			Intent in = new Intent();
//			Bundle bundle = new Bundle();
//			bundle.putInt("action_code", BaseActivity.PAY_CODE);
//			bundle.putSerializable("order", order);
//			in.putExtras(bundle);
//			in.setClass(ctx, PlatformActivity.class);
//			this.opc = opc;
//			ctx.startActivity(in);
//			return false;
//		}
		Intent in = new Intent();
		Bundle bundle = new Bundle();
		bundle.putInt("action_code", BaseActivity.PAY_CODE);
		bundle.putSerializable("order", order);
		in.putExtras(bundle);
		in.setClass(ctx, PlatformActivity.class);
		this.opc = opc;
		ctx.startActivity(in);
		return false;
	}
	public Boolean retrievePassword(Activity activity,String appid) {
			Intent in = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("appid", appid);
			in.putExtras(bundle);
			in.setClass(activity, RetrievePasswordActivity.class);
			activity.startActivityForResult(in, BaseActivity.RETRIEVEPASS_CODE);
			activity.overridePendingTransition(R.anim.activity_move,
				       R.anim.activity_move2);
			return true;
	}
	public Boolean showOrderList(Activity activity,String appid,String uid) {
		Intent in = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("appid", appid);
		bundle.putString("uid", uid);
		in.putExtras(bundle);
		in.setClass(activity, OrderListActivity.class);
		activity.startActivityForResult(in, BaseActivity.RETRIEVEPASS_CODE);
		activity.overridePendingTransition(R.anim.activity_move,
			       R.anim.activity_move2);
		return true;
	}
}
